<template>
  <div>
    <HomeHeader></HomeHeader>
    <HomeTu :swi="swiperList"></HomeTu>
    <HomeList :nav="navList"></HomeList>
    <HomeRecommend :re="reList"></HomeRecommend>
    <HomeWeekend :ww="weekList"></HomeWeekend>
  </div>
</template>

<script>
import HomeHeader from "./components/HomeHeader";
import HomeTu from "./components/homeTu";
import HomeList from "./components/HomeList";
import HomeRecommend from "./components/recommend";
import HomeWeekend from "./components/Weekend";
import axios from "axios";
import { mapState } from "vuex";
export default {
  name: "Home",
  components: {
    HomeHeader,
    HomeTu,
    HomeList,
    HomeRecommend,
    HomeWeekend
  },
  data() {
    return {
      // city:'',
      lastCity: "",
      swiperList: [],
      navList: [],
      reList: [],
      weekList: []
    };
  },
  computed: {
    ...mapState(["city"])
  },
  methods: {
    getHome() {
      axios.get("/api/index.json?city=" + this.city).then(this.getHomeInfoSucc);
    },
    getHomeInfoSucc(res) {
      res = res.data;
      console.log(res.data);
      // if (res.ret && res.data) {

      // this.city = res.data.city
      this.swiperList = res.data.swiperList;
      this.navList = res.data.iconList;
      this.reList = res.data.recommendList;
      this.weekList = res.data.weekendList;
      // }
    }
  },
  mounted() {
    // console.log("mounted")
    this.lastCity = this.city;
    this.getHome();
  },
  activated() {
    if (this.lastCity !== this.city) {
      this.lastCity = this.city;
      this.getHome();
    }
    console.log("activated");
  },
  beforeRouteLeave(to, from, next) {
    //从这个页面出去的时候执行这里
    this.scrollTop =
      document.documentElement.scrollTop || document.body.scrollTop; // 得到当前高
    // this.$store.commit("top", this.scrollTop);
    next();
  },

  beforeRouteEnter(to, from, next) {
    next(vm => {
      console.log(vm);
      document.documentElement.scrollTop = vm.scrollTop;
      document.body.scrollTop = vm.scrollTop;
    });
  }
};
</script>

<style lang="stylus" scoped></style>